package cn.xu.demo;

import java.util.ArrayList;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.junit.Test;

public class DemoJson {
	
	/*
	 * 当map来使用
	 */
	@Test
	public void test1(){
		JSONObject map = new JSONObject();
		map.put("name", "张三");
		map.put("age", "18");
		map.put("sex", "男");
		System.out.println(map.toString());
	}
	
	/*
	 * 可以将一个对象转化为一个json对象
	 */
	@Test
	public void test2(){
		Person p1 = new Person("王五",20,"男");
		JSONObject map = JSONObject.fromObject(p1);
		System.out.println(map.toString());
	}
	
	/*
	 * JSONArray
	 */
	@Test
	public void test3(){
		Person p1 = new Person("1",2,"3");
		Person p2 = new Person("4",5,"6");
		
		JSONArray list = new JSONArray();
		list.add(p1);
		list.add(p2);
		System.out.println(list.toString());
	}
	
	/*
	 * 本来就有一个 list 将list转换为 JSONArray
	 */
	@Test
	public void test4(){
		Person p1 = new Person("1",2,"3");
		Person p2 = new Person("4",5,"6");
		ArrayList list = new ArrayList();
		list.add(p1);
		list.add(p2);
		JSONArray jsonList = JSONArray.fromObject(list);
		System.out.println(jsonList.toString());
		
		
	}
	

}
